package com.kma.ncpractice2013.reports;

import com.kma.ncpractice2013.dao.Connector;
import com.kma.ncpractice2013.dao.HotelDAO;
import com.kma.ncpractice2013.dao.OrderDAO;
import com.kma.ncpractice2013.model.Order;
import org.json.JSONArray;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * User: Viktor
 * Date: 11/4/13
 */

public class ReportServlet extends HttpServlet
{
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		response.setContentType("application/json");
		//response.setCharacterEncoding("UTF-8");

		String rep_type = request.getParameter("report_type");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		Date start = Date.valueOf(startDate);
		Date end = Date.valueOf(endDate);
		//int page = Integer.parseInt(request.getParameter("page"));
		PrintWriter out = response.getWriter();


		if (rep_type.equals("top_dest"))
		{
			try
			{
				response.getWriter().write(getTopDest(start, end));
			} catch (SQLException e)
			{
				response.sendRedirect("/error.jsp?err=sqlerr");
			}
		}
		else if (rep_type.equals("top_price"))
		{
			response.getWriter().write(getTopPrice(start, end));
			response.flushBuffer();
		}


	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{

	}
	private String getTopPrice(Date start, Date end)
	{
		OrderDAO orderDAO = new OrderDAO();
		List<Order> orderList = orderDAO.getAllOrderedTimeframe("price", start, end);


		HotelDAO hotelDAO = new HotelDAO();
		List<String> locationsList = new ArrayList<String>();
		for(Order o:orderList)
		{
			locationsList.add((hotelDAO.getByHotelId(o.getLocationId())).getName());
		}
		//int count = orderDAO.getCount();

		HashMap<String, List> map = new HashMap<String, List>();
		map.put("locationlist", locationsList);
		map.put("orderlist", orderList);


		return JSONObject.valueToString(map);


	}
	private String getTopDest(Date start, Date end) throws SQLException
	{
		javax.sql.DataSource ds = Connector.getDataSource();
		String sql = "SELECT hotels.name, hotel_id AS ids, OrderCount FROM (SELECT ORDERS.HOTEL_ID AS ids, COUNT(orders.id)" +
				             " AS OrderCOunt FROM Orders where orders.date_of_order > ? and orders.date_of_order< ? GROUP BY orders.hotel_id )" +
				             " JOIN hotels ON hotels.hotel_id = ids ORDER BY OrderCount DESC";
		ResultSet rs;

		Connection conn = ds.getConnection();
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setDate(1, start);
		ps.setDate(2, end);
		rs = ps.executeQuery();
		List<ReportDestMap> resList = new ArrayList<ReportDestMap>();
		ReportDestMap map;
		while(rs.next())
		{
			map = new ReportDestMap();
			map.setName(rs.getString("name"));
			map.setId(rs.getInt("ids"));
			map.setCount(rs.getInt("ordercount"));
			resList.add(map);
		}
		JSONArray array = new JSONArray(resList);


		return array.toString();
	}
}
